// 解析为pageStyle
export const decodePageStyle = function (style) {
  // 默认设置
  if (style.fillType === 1) style.backgroundImage = '';
  style.gradientModel = style.gradientModel || {
    colors: ['rgba(232, 120, 16, 1)', 'rgba(141, 99, 232, 1)'],
    rotate: 0
  };
  style.backgroundRepeat = style.backgroundRepeat || 'no-repeat';
  style.opacity = style.opacity === undefined ? 1 : style.opacity;
  return style;
};

// 转换PageStyle
export const encodePageStyle = function (pageStyle) {
  const { fillType, backgroundRepeat, gradientModel, backgroundImage, opacity } = pageStyle;
  const style = { fillType };
  switch (fillType) {
    case 0:
      style.backgroundColor = pageStyle.backgroundColor;
      break;
    case 1:
      style.backgroundColor = null;
      style.gradientModel = gradientModel;
      style.backgroundImage = `linear-gradient(${gradientModel.rotate}deg,${gradientModel.colors[1]},${gradientModel.colors[0]})`;
      break;
    case 2:
      style.backgroundColor = null;
      style.backgroundImage = backgroundImage;
      style.backgroundRepeat = backgroundRepeat;
      style.backgroundSize = backgroundRepeat === 'no-repeat' ? 'cover' : '100% auto';
      style.backgroundPosition = 'center';
      style.opacity = opacity;
      break;
    default:
      break;
  }
  return style;
};

// 解析为BackgroundStyle
export const decodeBackgroundStyle = function (style) {
  // 默认设置
  if (style.fillType === 1) style.backgroundImage = '';
  style.gradientModel = style.gradientModel || {
    colors: ['rgba(232, 120, 16, 1)', 'rgba(141, 99, 232, 1)'],
    rotate: 0
  };
  style.backgroundRepeat = style.backgroundRepeat || 'no-repeat';
  return style;
};

// 转换BackgroundStyle
export const encodeBackgroundStyle = function (BackgroundStyle) {
  const { fillType, backgroundColor, backgroundRepeat, gradientModel, backgroundImage } = BackgroundStyle;
  const style = { fillType };
  switch (fillType) {
    case 0:
      style.backgroundColor = backgroundColor;
      break;
    case 1:
      style.gradientModel = gradientModel;
      style.backgroundImage = `linear-gradient(${gradientModel.rotate}deg,${gradientModel.colors[1]},${gradientModel.colors[0]})`;
      break;
    case 2:
      style.backgroundImage = backgroundImage;
      style.backgroundRepeat = backgroundRepeat;
      style.backgroundSize = backgroundRepeat === 'no-repeat' ? 'cover' : '100% auto';
      style.backgroundPosition = 'center';
      break;
    default:
      break;
  }
  return style;
};
